﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Threading;

namespace TestMultiThreading
{
    /// <summary>
    /// Interaction logic for Main.xaml
    /// </summary>
    public partial class Main : Window
    {
        private BackgroundWorker myBgw ;
        public Main()
        {
            InitializeComponent();
            
            InitializeBcgworker();

        }

        private void InitializeBcgworker()
        {
            myBgw = new BackgroundWorker();
            myBgw.WorkerSupportsCancellation = true;
            myBgw.WorkerReportsProgress = true;
            myBgw.DoWork += new DoWorkEventHandler(myBgw_DoWork);
            myBgw.ProgressChanged += new ProgressChangedEventHandler(myBgw_ProgressChanged);
            myBgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(myBgw_RunWorkerCompleted);
            myBgw.Disposed += new EventHandler(myBgw_Disposed);
            
        }

        void myBgw_Disposed(object sender, EventArgs e)
        {
            lblBgwstatus.Content = "Dispose";
            Thread.Sleep(3000);
        }

        void myBgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            lblBgwstatus.Content = "Completed";
            Thread.Sleep(3000);
        }

        void myBgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            lblBgwstatus.Content = (string)e.UserState;
            pgbBgkWorker.Value = e.ProgressPercentage;
         }

        private void myBgw_DoWork(object sender, DoWorkEventArgs e)
        {
            
            
            BackgroundWorker worker = (BackgroundWorker)sender;
          
            e.Result = CalculTest(worker,e);

        }

        private object CalculTest(BackgroundWorker worker, DoWorkEventArgs e)
        {
            Thread.Sleep(1000);
            worker.ReportProgress(10, "Is working ... 10%");
            Thread.Sleep(1000);
            worker.ReportProgress(20, "Is working ... 20%");
            Thread.Sleep(1000);
            worker.ReportProgress(30, "Is working ... 30%");
            Thread.Sleep(1000);
            worker.ReportProgress(40, "Is working ... 40%");
            Thread.Sleep(1000);
            worker.ReportProgress(50, "Is working ... 50%");
            Thread.Sleep(1000);
            worker.ReportProgress(60, "Is working ... 60%");
            Thread.Sleep(1000);
            worker.ReportProgress(70, "Is working ... 70%");
            Thread.Sleep(1000);
            worker.ReportProgress(80, "Is working ... 80%");
            Thread.Sleep(1000);
            worker.ReportProgress(90, "Is working ... 90%");
            Thread.Sleep(1000);
            worker.ReportProgress(99, "Is working ... 99%");
            Thread.Sleep(1000);
            worker.ReportProgress(100, "Is working ... 100%");
            return 0;
        }




        private void btStartBkworker_Click(object sender, RoutedEventArgs e)
        {
            lblBgwstatus.Content = "Thread is start";
        
            myBgw.WorkerReportsProgress = true;
            myBgw.RunWorkerAsync();
            

            
        }
    }
}
